package com.code.background.drawable;

import android.content.res.ColorStateList;
import android.content.res.TypedArray;


import com.code.background.R;

import androidx.annotation.AttrRes;

public class ColorStateCreator implements ICreateColorState{

    private final TypedArray mTypedArray;

    ColorStateCreator(TypedArray mTypedArray) {
        this.mTypedArray = mTypedArray;
    }

    private int[][] mStates = new int[][]{};
    private int[] mColors = new int[]{};
    private int mIndex;

    public ColorStateList create() {
        mStates = new int[mTypedArray.getIndexCount()][];
        mColors = new int[mTypedArray.getIndexCount()];
        for (int i = 0; i < mTypedArray.getIndexCount(); i++) {
            int attr = mTypedArray.getIndex(i);
            if (attr == R.styleable.text_selector_checkable_textColor) {
                setStateColor(mTypedArray, attr, android.R.attr.state_checkable);
            } else if (attr == R.styleable.text_selector_unCheckable_textColor) {
                setStateColor(mTypedArray, attr, -android.R.attr.state_checkable);
            } else if (attr == R.styleable.text_selector_checked_textColor) {
                setStateColor(mTypedArray, attr, android.R.attr.state_checked);
            } else if (attr == R.styleable.text_selector_unChecked_textColor) {
                setStateColor(mTypedArray, attr, -android.R.attr.state_checked);
            } else if (attr == R.styleable.text_selector_enabled_textColor) {
                setStateColor(mTypedArray, attr, android.R.attr.state_enabled);
            } else if (attr == R.styleable.text_selector_unEnabled_textColor) {
                setStateColor(mTypedArray, attr, -android.R.attr.state_enabled);
            } else if (attr == R.styleable.text_selector_selected_textColor) {
                setStateColor(mTypedArray, attr, android.R.attr.state_selected);
            } else if (attr == R.styleable.text_selector_unSelected_textColor) {
                setStateColor(mTypedArray, attr, -android.R.attr.state_selected);
            } else if (attr == R.styleable.text_selector_pressed_textColor) {
                setStateColor(mTypedArray, attr, android.R.attr.state_pressed);
            } else if (attr == R.styleable.text_selector_unPressed_textColor) {
                setStateColor(mTypedArray, attr, -android.R.attr.state_pressed);
            } else if (attr == R.styleable.text_selector_focused_textColor) {
                setStateColor(mTypedArray, attr, android.R.attr.state_focused);
            } else if (attr == R.styleable.text_selector_unFocused_textColor) {
                setStateColor(mTypedArray, attr, -android.R.attr.state_focused);
            } else if (attr == R.styleable.text_selector_activated_textColor) {
                setStateColor(mTypedArray, attr, android.R.attr.state_activated);
            } else if (attr == R.styleable.text_selector_unActivated_textColor) {
                setStateColor(mTypedArray, attr, -android.R.attr.state_active);
            } else if (attr == R.styleable.text_selector_active_textColor) {
                setStateColor(mTypedArray, attr, android.R.attr.state_active);
            } else if (attr == R.styleable.text_selector_unActive_textColor) {
                setStateColor(mTypedArray, attr, -android.R.attr.state_activated);
            } else if (attr == R.styleable.text_selector_expanded_textColor) {
                setStateColor(mTypedArray, attr, android.R.attr.state_expanded);
            } else if (attr == R.styleable.text_selector_unExpanded_textColor) {
                setStateColor(mTypedArray, attr, -android.R.attr.state_expanded);
            }

        }
        return new ColorStateList(mStates, mColors);
    }

    private void setStateColor(TypedArray selectorTa, int attr, @AttrRes int functionId) {
        mStates[mIndex] = new int[]{functionId};
        mColors[mIndex] = selectorTa.getColor(attr, 0);
        mIndex++;
    }
}
